package com.hspedu.newData.dmsxl.数组.区间;

import java.util.*;

/**
 * @Author: yh
 * @description: 
 * @CreateTime: 2025-05-09 
 * @Version: 1.0
 */


// todo 非常重要而且不会写
public class LC56合并区间 {

    public int[][] merge(int[][] intervals) {

        if (intervals.length == 0){
            return new int[0][2];
        }

        Arrays.sort(intervals, new Comparator<int[]>() {
            @Override
            public int compare(int[] o1, int[] o2) {
                return o1[0] - o2[0];
            }
        });

        List<int[]> merged = new ArrayList<>();
        for (int i = 0; i < intervals.length; i++) {
            int L = intervals[i][0],R = intervals[i][1];
            if (merged.size()==0 || merged.get(merged.size() - 1)[1]<L){
                merged.add(new int[]{L,R});
            }else{
                merged.get(merged.size() - 1)[1] = Math.max(R,merged.get(merged.size() - 1)[1]);
            }
        }
        return merged.toArray(new int[merged.size()][2]);
    }
}
